package client_commands;

import java.util.StringTokenizer;

import utils.Constants;

/**
 * Klasa koja predstavlja parser komande.
 * 
 * @author UrosKrkic & Radivoje Popovic
 */
public class CommandFactory implements Constants {

	
	/**
	 * Staticki metod koji parsira komandu. Na osnovu stringa koji predstavlja
	 * komandu (i njen parametar), vrsi se parsiranje ovih vrednosti, i na osnovu
	 * toga se keira odgovarajuca komanda.
	 * @param commandString - string koji sadrzi ime komande i eventualno njen
	 * parametar
	 * @return - kreirana komanda
	 * @author UrosKrkic & Radivoje Popovic
	 */
	public static Command createCommand(String commandString) {
		
		if (commandString == null)
			return null;
		
		Command command;
		String cmd = "";
		String parameter = null;
		StringBuffer tempParameter = new StringBuffer();
		int code = -1;
		
		StringTokenizer tokens = new StringTokenizer(commandString);
		
		if (tokens.hasMoreTokens()) {
			cmd = tokens.nextToken();
		}
		while (tokens.hasMoreTokens()) {
			tempParameter.append(tokens.nextToken() + " ");
		}
		if ( tempParameter.length() != 0) {
			int spIndex = tempParameter.lastIndexOf(" ");
			tempParameter.deleteCharAt(spIndex);
			parameter = tempParameter.toString();
		}
		
		for (int i = 0; i < CLIENT_COMMAND_NAMES.length; i++) {
			if ( cmd.toUpperCase().equals(CLIENT_COMMAND_NAMES[i]) ) {
				code = i;
				break;
			}
		}
		
		
		switch ( code ) {
			case SERV_LOGIN_CMD:
				command = new SERV_LOGINCommand(parameter);
				return command;
			case SERV_LOGOUT_CMD:
				command = new SERV_LOGOUTCommand(parameter);
				return command;
			case SERV_CHNICK:
				command = new SERV_CHNICKCommand(parameter);
				return command;
			case SERV_CHAVATAR:
				command = new SERV_CHAVATARCommand(parameter);
				return command;
			case SERV_CHSTATUS:
				command = new SERV_CHSTATUSCommand(parameter);
				return command;
			case SERV_CHMAIL:
				command = new SERV_CHMAILCommand(parameter);
				return command;
			case SERV_UNREG:
				command = new SERV_UNREGCommand(parameter);
				return command;
			case SERV_ADDED:
				command = new SERV_ADDEDCommand(parameter);
				return command;
//			case SERV_BLOCK:
//				command = new SERV_BLOCKCommand(parameter);
//				return command;
//			case SERV_UNBLOCK:
//				command = new SERV_UNBLOCKCommand(parameter);
//				return command;
			case SERV_DEL:
				command = new SERV_DELCommand(parameter);
				return command;
			case CLIENT_SEND_MSG:
				command = new CLIENT_SEND_MSGCommand(parameter);
				return command;
//			case SET_SERVER_INFO:
//				command = new SET_SERVER_INFOCommand(parameter);
//				return command;
			case PEER_TYPING:
				command = new PEER_TYPINGCommand(parameter);
				return command;
			case SERV_PING:
				command = new SERV_PINGCommand(parameter);
				return command;
			case CALL_REQUEST:
				command = new CALL_REQUESTCommand(parameter);
				return command;
			case STOP_CALL:
				command = new STOP_CALLCommand(parameter);
				return command;
		}
		
		return null;
	}
	
}
